<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class shopcartController extends Controller
{
    // 购物车
     public function index($pid){
    	// 查询商品信息
    	// pid 是商品id
    	// amount 是购买商品数量
		$detail = DB::table('detail')->where('id',$pid)->select('price')->get()->first();		
		// 获取购物车商品总价
		$sum = $detail->price * $_GET['amount']; 		
    	$did = DB::table('order')->insertGetId(
    		['pid' => $pid, 'amount' => $_GET['amount'], 'uname' => session('uname'), 'money' => $sum]
    	);    	
    	if($did){
    		return back();
    	}
    }

    public function cart(){


        $list = DB::table('order')
            ->where('state', 1)
            ->join('detail', 'detail.id', '=', 'order.pid')
            ->select('detail.*', 'order.*')
            ->get()->all();
        session(['list' => $list]);
            
        
        if(count($list) < 1){
            // 判断购物车里有没有货物，如果没有，就跳回原来页面           
            return view('home.noShopcart');
        }        
        $price = DB::table('order')
            ->where('state', 1)
            ->join('detail', 'detail.id', '=', 'order.pid')
            ->select('detail.*', 'detail.title', 'detail.pic')
            ->get()->all();
        return view('home.shopcart', ['list' => $list, 'price' => $price]);
    }
}